<template>
  <el-menu class="top-menu"
           :ellipsis="false"
           :default-active="SysId"
           mode="horizontal"
           text-color="#333">
    <template v-for="(item,index) in items"
              :key="index">
      <el-menu-item v-if="item.children && item.children.length === 0" :index="item.PLATSYSID+''"
                    @click="openMenu(item)">
        <template #title>
          <i class="icon-caidan"></i>
          <span>{{ generateTitle(item) }}</span>
        </template>
      </el-menu-item>
      <el-sub-menu v-if="item.children && item.children.length > 0" :index="item.PLATSYSID">
        <template #title>
          <span>{{ generateTitle(item) }}</span>
        </template>
        <template v-for="child in item.children">
          <el-menu-item :index="child.PLATSYSID + ''" @click="openMenu(child)"
                        class="top-menu-item"
          >
            <template #title>
              <i class="icon-caidan"></i>
              <span>{{ generateTitle(child) }}</span>
            </template>
          </el-menu-item>
        </template>
      </el-sub-menu>
    </template>
  </el-menu>
</template>

<script>
import {mapGetters} from "vuex";

export default {
  name: "top-menu",
  data() {
    return {
      items: []
    };
  },
  inject: ["index"],
  created() {
    this.getMenu();
  },
  computed: {
    ...mapGetters(["tagCurrent", "menu", 'menuAll', 'SysId'])
  },
  methods: {
    openMenu(item) {
      this.index.openMenu(item)
    },
    getMenu() {
      this.$store.dispatch("GetTopMenu").then(res => {
        this.items = res;
        if (!this.SysId || this.menuAll.length === 0) {
          this.index.openMenu(res[0])
        }
      });
    },
    generateTitle(item) {
      return this.$router.$avueRouter.generateTitle(item);
    }
  }
};
</script>
